package executable.knapsack;

import ctu.kfad.run.util.FileUtil;
import ctu.kfad.run.util.StringUtil;

public class Knapsack {

    public static void main(String[] args) {
        if (args.length != 2) {
            StringUtil.print("Two arguments are required. 1 - task file path , 2 - result path");
            return;
        }
        String input = FileUtil.readToString(args[0]);
        int result = run(input);
        final String content = StringUtil.convertFromInt(result);
        FileUtil.writeTo(args[1], StringUtil.concatenate("The result is: ", content));
    }


    public static int run(String input) {
        return run(StringUtil.split(input, " "));
    }

    public static int run(String[] input) {
        final int[] intInput = new int[input.length];
        for (int i = 0; i < input.length; i++) {
            intInput[i] = StringUtil.convertToInt(input[i]);
        }
        return run(intInput);
    }

    public static int run(int[] input) {
        Item[] items = new Item[input[0]];
        int maxWeight = input[1];
        for (int i = 2, index = 0; i < input.length; i = i + 2, index++) {
            Item item = new Item(input[i], input[i + 1]);
            items[index] = item;
        }
        BruteForceKnapsack bruteForceKnapsack = new BruteForceKnapsack(items, maxWeight);
        return bruteForceKnapsack.compute();
    }
}
